package cn.woochen.mvvmframe.viewmodels

import cn.woochen.comlib.BaseViewModel
import cn.woochen.mvvmframe.bean.ArticleBean
import cn.woochen.mvvmframe.bean.HomeBannerBean
import cn.woochen.mvvmframe.config.ext.request
import cn.woochen.mvvmframe.config.status.ResultState
import cn.woochen.mvvmframe.util.rxApi
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.flow.asStateFlow

class NetViewModel : BaseViewModel() {
    /**
     * MutableStateFlow在数据没有改变时不会回调
     */
    private val _homeBannerMutable = MutableStateFlow<ResultState<List<HomeBannerBean>>>(ResultState.Empty())
    val homeBannerInfo = _homeBannerMutable.asStateFlow()

    /**
     * MutableSharedFlow在数据没有改变时仍然会回调
     */
    private val _articleListMutable = MutableSharedFlow<ResultState<ArticleBean>>()
    val articleListInfo = _articleListMutable.asSharedFlow()

    fun homeBanner() {
        request({ rxApi.homeBanner() }, _homeBannerMutable, false)
    }

    fun articleList(page: Int, pageSize: Int) {
        request({ rxApi.articleList(page, pageSize) }, _articleListMutable, true)
    }
}